﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace LiteQuery.Controls
{
    public partial class FieldDateComparePanel : UserControl
    {
        public event EventHandler OnValueChanged;

        Entities.Field field;
        public Entities.Field FieldInfo {
            get {
                if (field == null)
                    field = new Entities.Field();
                field.Value = LiteQuery.Common.DateHelper.DateToTimestamp( dtValue.Value).ToString() ;
                field.Operator = ((Entities.Operator)cboOperator.SelectedItem).Value;
                return field;
            }
            set { field = value; }
        }

        public string Caption
        {
            get { return lblCaption.Text; }
            set {
                if (lblCaption.InvokeRequired)
                    lblCaption.Invoke(new Action(() => lblCaption.Text = value));
                else
                    lblCaption.Text = value;
            }
        }


        public FieldDateComparePanel()
        {
            InitializeComponent();
            LoadOperators ();
        }

        public FieldDateComparePanel(Entities.Field fi)
        {
            InitializeComponent();
            this.FieldInfo = fi;
            this.Caption = fi.Caption;
            LoadOperators();
        }

        private void LoadOperators()
        {
            cboOperator.Items.Add(new Entities.Operator { Caption="=", Value="="});
            cboOperator.Items.Add(new Entities.Operator { Caption="<>", Value="<>"});
            cboOperator.Items.Add(new Entities.Operator { Caption=">", Value=">"});
            cboOperator.Items.Add(new Entities.Operator { Caption="<", Value="<"});
            cboOperator.Items.Add(new Entities.Operator { Caption=">=", Value=">="});
            cboOperator.Items.Add(new Entities.Operator { Caption="<=", Value="<="});
            cboOperator.SelectedIndex = 0;
        }

        private void dtValue_ValueChanged(object sender, EventArgs e)
        {
            field.Value =  LiteQuery.Common.DateHelper.DateToTimestamp(dtValue.Value).ToString();
            field.Operator = ((Entities.Operator)cboOperator.SelectedItem).Value;
            if (OnValueChanged != null)
                OnValueChanged(this, null);
        }

        private void cboOperator_SelectedIndexChanged(object sender, EventArgs e)
        {
            field.Operator = ((Entities.Operator)cboOperator.SelectedItem).Value;
        }
    }
}
